﻿#include <time.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "MarioGame.h"
#include "Surface.h"
#include "Resource.h"
#include "Collision.h"

/**--------------------------------------------------------
*Description : Constructor
*Method name : MarioManager
*Parameters :  HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate
*Return value :
--------------------------------------------------------**/
MarioGame::MarioGame(HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate):
Game(hInstance,Name,Mode,IsFullScreen, FrameRate)
{
	//TODO: Init game
	quadtree = new QuadTree();
}


/**--------------------------------------------------------
*Description : Destructor
*Method name : ~MarioManager
*Parameters :  
*Return value :
--------------------------------------------------------**/
MarioGame::~MarioGame()
{
	//TODO: Release game
	delete camera;
	delete load_QTree;
	delete quadtree;



	delete dxAudio;

	delete backgroundSound;
	delete bonusRiseSound;
	delete brickBreakSound; 
	delete bumpSound;
	delete coinSound; 
	delete deathSound;
	delete fireballSound; 
	delete gameOverSound;
	delete jumpSound; 
	delete kickSound; 
	delete lifeSound; 
	delete mipSound; 
	delete pipeWarpSound; 
	delete powerupSound; 
	delete startGameSound; 
	delete winGameSound;
}


/**--------------------------------------------------------
*Description : Load tai nguyen 
*Method name : LoadResources
*Parameters :  LPDIRECT3DDEVICE9 d3ddv
*Return value :void
--------------------------------------------------------**/
void MarioGame::LoadResources(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer)
{
	HRESULT res = D3DXCreateSprite(d3ddv, &_spriteHandler);
	//TODO: Load resource game	
	ResourceMng *rm = ResourceMng::GetInst(d3ddv, backBuffer);

	num_Score = 0;
	num_Life = 3;
	num_Coin = 0;
	count_Time = 400;

	reset_Time = 0;

	map_Current = 1;
	submap = 0;

	subMapToMap = 0;
	isReplay = false;

	isLoadGame = 0;

	camera = new Camera();
	//map1 = new MapMario();
	//map1->LoadMapFromFile("..\\Resources\\Images\\savemap.txt");
	//map_list = map1->GetObjects();


	load_QTree = new LoadMap(d3ddv, backBuffer, _spriteHandler);
	load_QTree->loadFileObj("..\\Resources\\Map\\map.txt");
	load_QTree->loadFileToMap("..\\Resources\\Map\\map.txtquadtree.txt");
	load_QTree->deserialize(quadtree);

	
	_mario = new Mario(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_QTree->posX_first, load_QTree->posY_first, this, load_QTree->mario_state, load_QTree->mario_direction);
	//_mario = new Mario(d3ddv, backBuffer, _spriteHandler, load_QTree->posX_first, load_QTree->posY_first, this, load_QTree->mario_state, load_QTree->mario_direction);

	pointMenu = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\star.png"), 125, 0);
	game_State = GS_MENU;
	xPointMenu = 320;
	yPointMenu = 320;

	/*
	info score
	info coin
	info number life
	info time
	info world1
	info world2
	*/
	scoreArr[SCORE] = new Score(d3ddv, backBuffer, _spriteHandler, 100, 550);			 
	scoreArr[COIN] = new Score(d3ddv, backBuffer, _spriteHandler, 200, 550);			 
	scoreArr[NUMBER_LIFE] = new Score(d3ddv, backBuffer, _spriteHandler, 450, 285);		 
	scoreArr[NUMBER_LIFE]->Update(num_Life);
	scoreArr[TIME] = new Score(d3ddv, backBuffer, _spriteHandler, 700, 550);			 
	scoreArr[WORLD1] = new Score(d3ddv, backBuffer, _spriteHandler, 500, 550);			 
	scoreArr[WORLD1]->Update(map_Current);
	scoreArr[WORLD2] = new Score(d3ddv, backBuffer, _spriteHandler, 450, 250);			 
	scoreArr[WORLD2]->Update(map_Current);

	/*
	info num life
	info coin
	info world
	info time
	info result
	*/
	infoArr[0] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infomario.png"), 0, 0), 50, 580);	
	infoArr[1] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infocoin.png"), 0, 0), 150, 550);	
	infoArr[2] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infoworld.png"), 0, 0), 500, 580);	
	infoArr[3] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infotime.png"), 0, 0), 650, 580);	
	infoArr[4] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\inforesult.png"), 0, 0), 370, 300);
	infoArr[5] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infoworld.png"), 0, 0), 370, 250);
	//save
	infoArr[6] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\saveandquit.png"), 0, 0), 370, 280);
	infoArr[7] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\quitdontsave.png"), 0, 0), 370, 230);
	infoArr[8] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\dontquit.png"), 0, 0), 370, 180);


	/*audio = new Audio();
	if (!audio->Init())
	trace(L"[ERROR] Failed to create audio");
	if (!audio->Load("..\\Resources\\Audio\\gong.ogg", "gong"))
	trace(L"[ERROR] Failed to create file");
	if (!audio->Load("..\\Resources\\Audio\\explosion.wav", "explosion"))
	trace(L"[ERROR] Failed to create file");
	if (!audio->Load("..\\Resources\\Audio\\BackGround.mid", "BackGround"))
	trace(L"[ERROR] Failed to create file");
	wobble = new Sample();
	wobble = audio->Load("..\\Resources\\Audio\\wobble.wav");
	if (!wobble)
	trace(L"[ERROR] Failed to create file");*/

	//audio->PlayBg("BackGround");



	//load bounce wave file
	dxAudio = new DxAudio();
	dxAudio->Init_DirectSound(_hWnd);

	backgroundSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Background.wav");
	bonusRiseSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\BonusRise.wav");
	brickBreakSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\BrickBreaked.wav");
	bumpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Bump.wav");
	coinSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Coin.wav");
	deathSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Death.wav");
	fireballSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Fireball.wav");
	gameOverSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\GameOver.wav");
	jumpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Jump.wav");
	kickSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Kick.wav");
	lifeSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Life.wav");
	mipSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Mip.wav");
	pipeWarpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\PipeWarp.wav");
	powerupSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\PowerUp.wav");
	startGameSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\StartGame.wav");
	winGameSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\WinStage.wav");

	
}


/**--------------------------------------------------------
*Description : Render Frame
*Method name : RenderFrame
*Parameters :  LPDIRECT3DDEVICE9 d3ddv, int t
*Return value :void
--------------------------------------------------------**/
void MarioGame::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer, int t)
{
	//TODO: DRAW---------------------------------------------------------
	// Surface
	//GLDevice->StretchRect(...)
	_spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);


	switch (game_State)
	{
	case GS_MENU:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\menu.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		pointMenu->render(xPointMenu, yPointMenu);
		break;

	case GS_ABOUT:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\about.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_PAUSE:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\pause.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		pointMenu->render(xPointMenu, yPointMenu);
		infoArr[6]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[7]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[8]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		break;


	case GS_GAMEOVER:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\gameover.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_GAMEWIN:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\gamewin.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_SUB_STAGE:
		break;

	case GS_RETURN_STAGE:
		break;

	case GS_REPLAY:
	case GS_NEXT_STAGE:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, BackBuffer)->GetSurface(L"..\\Resources\\Images\\transfermap.png")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\background.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL

		scoreArr[SCORE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[COIN]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[NUMBER_LIFE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);

		infoArr[0]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[3]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[4]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[5]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_LOAD:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		break;

	case GS_PLAYING:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\background.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL


		//////////////////////////////////////////////////////////////////////////
		//list<int> obj_drawed;

		Object *obj;
		list<Object*>::iterator i;
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getObjectType() == OBJECT_SCENE || obj->getObjectType() == OBJECT_LAND)
			{
				if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + 800 + 30)
				{
					if (obj->getIDObjType() != 241)
					{
						obj->render(camera->getCamera().left, camera->getCamera().top);
					}
					
				}
				
			}			
		}
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getObjectType() == OBJECT_ITEM || obj->getObjectType() == OBJECT_ENEMY)
			{
				if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + 800 + 30)
				{
					
					obj->render(camera->getCamera().left, camera->getCamera().top);
				}
				
			}
			
		}
		//////////////////////////////////////////////////////////////////////////

		_mario->render(camera->getCamera().left, camera->getCamera().top);

		//ve ong lon nhat co the chui dc
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getIDObjType() == 241)
			{
				if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + 800 + 30)
				{
					obj->render(camera->getCamera().left, camera->getCamera().top);
				}
			}
		}

		Score* tempScore;
		list<Score*>::iterator iScore;
		for (iScore = scoreList.begin(); iScore != scoreList.end(); iScore++)
		{
			tempScore = *iScore;
			tempScore->Render(camera->getCamera().left, camera->getCamera().top);
		}


		infoArr[0]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[3]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);


		scoreArr[SCORE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[COIN]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[TIME]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		break;

	
	}


	



	_spriteHandler->End();
}


/**--------------------------------------------------------
*Description : Kiem tra phim nhan
*Method name : ProcessInput
*Parameters :  LPDIRECT3DDEVICE9 d3ddv, int t
*Return value :void
--------------------------------------------------------**/
void MarioGame::ProcessInput(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	//printf("FPS: %f", this->GetFPS());

	//State
	//Nhan phim va giu
	//if (IsKeyDown(DIK_RIGHT))
	//{
	//	_mario->turnRight();
	//	//audio->Play("gong");
	//}	
	//else if (IsKeyDown(DIK_LEFT))
	//{
	//	_mario->turnLeft();
	//	//audio->Play("explosion");
	//}
	//else
	//{
	//	_mario->reset();
	//}

	//int input_key = p_input->IsKeyDown();
	/*if (p_input->IsKeyDown(DIK_ESCAPE))
	{
		trace(L"Main game is ended!");
		PostMessage(_hWnd,WM_QUIT,0,0);
	}*/

	switch (game_State)
	{
	case GS_MENU:
		{
			int temp = p_input->GetKeyDown();
			switch (temp)
			{
			case DIK_DOWN:
				yPointMenu += 50;
				if (yPointMenu > 470)
				{
					yPointMenu = 320;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_UP:
				yPointMenu -= 50;
				if (yPointMenu < 320)
				{
					yPointMenu = 470;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_RETURN:	//key Enter
				dxAudio->PlaySoundW(fireballSound);
				if (yPointMenu == 320)	//New Game
				{
					num_Coin = 0;
					num_Life = 3;
					num_Score = 0;
					
					scoreArr[NUMBER_LIFE]->Update(num_Life);
					scoreArr[COIN]->Update(num_Coin);
					scoreArr[SCORE]->Update(num_Score);

					map_Current = 1;
					ChangeMap(map_Current);
					//ChangeState(GS_PLAYING);

				}
				else if(yPointMenu == 370)	//Load Game
				{
					ChangeState(GS_LOAD);
				}
				else if(yPointMenu == 420)	//Game About
				{
					ChangeState(GS_ABOUT);
				}
				else if (yPointMenu == 470)	//Quit Game
				{
					//PostQuitMessage(0);
					trace(L"Main game is ended!");
					PostMessage(_hWnd,WM_QUIT,0,0);
				}
				break;

			}
		}
		break;

	case GS_PAUSE:
		{
			int temp = p_input->GetKeyDown();
			switch (temp)
			{
			case DIK_DOWN:
				yPointMenu += 50;
				if (yPointMenu > 420)
				{
					yPointMenu = 320;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_UP:
				yPointMenu -= 50;
				if (yPointMenu < 320)
				{
					yPointMenu = 420;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_RETURN:	//key Enter
				dxAudio->PlaySoundW(fireballSound);
				if (yPointMenu == 320)	//Save Game + Quit
				{
					//Save Game
					ChangeState(GS_SAVE);
										
				}
				else if(yPointMenu == 370)	//Quit Game - Don't Save
				{
					ChangeState(GS_MENU);
				}
				else if(yPointMenu == 420)	//Don't Quit Game
				{
					ChangeState(GS_PLAYING);
				}
				
				break;

			}
		}
		break;

	case GS_PLAYING:
		{
			if (p_input->IsKeyDown(DIK_ESCAPE))
			{
				
				ChangeState(GS_PAUSE);
				
			}

			//if (p_input->IsKeyDown(DIK_S))	//Save Map
			//{
			//	//chuyen trang thai tam dung
			//	ChangeState(GS_PAUSE);

			//	//load_QTree->numScore = num_Score;
			//	//load_QTree->numLife = num_Life;
			//	//load_QTree->numCoin = num_Coin;
			//	//
			//	////cap nhat lai vi tri, trang thai, huong di chuyen mario trong map
			//	//load_QTree->posX_first = (int)(_mario->getPosX());
			//	//load_QTree->posY_first = (int)(_mario->getPosY() + 2);
			//	//load_QTree->mario_state = _mario->getObjState();
			//	//load_QTree->mario_direction = _mario->getDirection();
			//	//			
			//	//quadtree->getObjList(&obj_list_save);				
			//	//load_QTree->saveFileObj("..\\Resources\\Map\\savemapfix.txt", obj_list_save);
			//	//dxAudio->PlaySoundW(fireballSound);
			//	//obj_list_save.clear();
			//}
		}


		break;

	case GS_ABOUT:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;

	case GS_GAMEOVER:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;

	case GS_GAMEWIN:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;
	}
}


/**--------------------------------------------------------
*Description : Kiem tra phim nhan nhay
*Method name : OnKeyDown
*Parameters :  int KeyCode
*Return value :void
--------------------------------------------------------**/
//void MarioGame::OnKeyDown(int KeyCode)
//{
//	//Buffer
//	// 
//	switch (KeyCode)
//	{
//		case DIK_SPACE:
//			_mario->jump();
//			//audio->Play(wobble);
//			break;
//
//		case DIK_UP:
//			_mario->jump();
//			//audio->Play(wobble);
//			break;
//	}
//}


/**--------------------------------------------------------
*Description : Kiem tra phim nhan nhay
*Method name : OnKeyDown
*Parameters :  int KeyCode
*Return value :void
--------------------------------------------------------**/
//void MarioGame::OnKeyUp(int KeyCode)
//{
//	//Buffer
//	// 
//	switch (KeyCode)
//	{
//		case DIK_SPACE:
//			 
//			break;
//	}
//}

void MarioGame::update(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, int Delta)
{
	//////////////////////////////////////////////////////////////////////////

	switch (game_State)
	{
	case GS_MENU:
		break;

	case GS_ABOUT:
		break;

	case GS_PAUSE:
		break;
	case GS_SAVE:
		load_QTree->numScore = num_Score;
		load_QTree->numLife = num_Life;
		load_QTree->numCoin = num_Coin;

		//cap nhat lai vi tri, trang thai, huong di chuyen mario trong map
		load_QTree->posX_first = (int)(_mario->getPosX());
		load_QTree->posY_first = (int)(_mario->getPosY() + 2);
		load_QTree->mario_state = _mario->getObjState();
		load_QTree->mario_direction = _mario->getDirection();

		quadtree->getObjList(&obj_list_save);		
		load_QTree->saveFileObj("..\\Resources\\Map\\savemapfix.txt", obj_list_save);
		dxAudio->PlaySoundW(fireballSound);
		obj_list_save.clear();

		ChangeState(GS_MENU);
		break;

	case GS_NEXT_STAGE:				//Khi đổi màn
		reset_Time += Delta;
		if (reset_Time > 100)
		{
			reset_Time -= 100;
			timeChangeMap--;
			if (timeChangeMap <= 0)
			{

				map_Current++;
				scoreArr[WORLD1]->Update(map_Current);
				scoreArr[WORLD2]->Update(map_Current);
				ChangeMap(map_Current);
			}
		}
		//ChangeMap(map_Current+1);
		break;

	case GS_RETURN_STAGE:				//Khi đổi màn
		reset_Time += Delta;
		if (reset_Time > 100)
		{
			reset_Time -= 100;
			timeChangeMap--;
			if (timeChangeMap <= 0)
			{
				//subMapToMap = 1;
				//int i = 0;
				ChangeMap(map_Current);
			}
		}
		//ChangeMap(map_Current+1);
		break;

	case GS_REPLAY:				//choi lai map
		reset_Time += Delta;
		if (reset_Time > 100)
		{
			reset_Time -= 100;
			timeChangeMap--;
			if (timeChangeMap <= 0)
			{
				isReplay = true;
				ChangeMap(map_Current);
			}
		}
		//ChangeMap(map_Current+1);
		break;

	case GS_SUB_STAGE:				//choi map phu
		reset_Time += Delta;
		if (reset_Time > 100)
		{
			reset_Time -= 100;
			timeChangeMap--;
			if (timeChangeMap <= 0)
			{
				submap = map_Current;
				
				ChangeSubMap(submap);
			}
		}
		break;

	case GS_PLAYING:
		reset_Time += Delta;
		if (reset_Time > 500)
		{
			reset_Time = 0;//500;
			count_Time--;
			scoreArr[TIME]->Update(count_Time);
			if (count_Time <= 0)
			{
				ChangeState(GS_GAMEOVER);
			}
		}

		Object* obj;
		list<Object*>::iterator i;
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			//neu obj k ton tai
			//xoa obj tu quadtree

		}
		obj_list.clear();


		RECT rect_camera = camera->getCamera();

		quadtree->getObjFromCamara(rect_camera, &obj_list);

		int ji = 0;

		

		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + 800 + 30)
			{
				obj->update(&obj_list, p_input, Delta);		
			}
			//obj->update(&obj_list, p_input, Delta);		
		}

		_mario->update(&obj_list, p_input, Delta);
		camera->update(_mario->getPosition());

		Score* tempScore;
		list<Score*>::iterator iScore;
		//HIEN THI DIEM KHI DAP CHET QUAI, AN DIEM
		scoreRemoveList.clear();
		for (iScore = scoreList.begin(); iScore != scoreList.end(); iScore++)
		{
			tempScore = *iScore;
			tempScore->update(&obj_list, p_input, Delta);
			if (tempScore->getObjState() == IS_IDLE) //die
			{
				delete tempScore;
				scoreRemoveList.push_back(tempScore);
			}
		}
		for (iScore = scoreRemoveList.begin(); iScore != scoreRemoveList.end(); iScore++)
		{
			scoreList.remove(*iScore);
		}
		break;
	}

	




}




void MarioGame::InfoScore(int score, int x, int y)
{
	num_Score += score;
	Score* _tempScore = new Score(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, x, y);
	_tempScore->score = score;
	scoreList.push_back(_tempScore);
}

void MarioGame::ChangeState(int state)
{
	game_State = state;
	switch (game_State)
	{
	case GS_MENU:
		xPointMenu = 320;
		yPointMenu = 320;
		break;

	case GS_PLAYING:
		count_Time = 400;
		reset_Time = 0;

		xPointMenu = 320;
		yPointMenu = 320;
		break;

	case GS_LOAD:
		 

		delete quadtree;
		quadtree = new QuadTree();
		delete load_QTree;
		load_QTree = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler);
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\savemapfix.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\map.txtquadtree.txt");
		load_QTree->deserialize(quadtree);

		//mapManager->isLoadSubMap = false;
		//obj_list.clear();

		//delete _mario;
		if (isLoadGame == 0)
		{
			_mario = new Mario(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_QTree->posX_first, load_QTree->posY_first, this, load_QTree->mario_state, load_QTree->mario_direction);
		}
		else
		{
			//delete _mario;
			_mario = new Mario(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_QTree->posX_first, load_QTree->posY_first, this, load_QTree->mario_state, load_QTree->mario_direction);
		}

		isLoadGame++;
		//_mario->setPosX(load_QTree->posX_first);
		//_mario->setPosY(load_QTree->posY_first);
		//_mario->setObjState(load_QTree->mario_state);
		//
		//if (load_QTree->mario_isSmall == 0)
		//{
		//	_mario->setIsSmall(true);	//nho
		//}
		//else
		//{
		//	_mario->setIsSmall(false);	//lon
		//}
		//_mario->changeState(load_QTree->mario_state);


		num_Score = load_QTree->numScore;
		num_Life = load_QTree->numLife;
		num_Coin = load_QTree->numCoin;
		scoreArr[SCORE]->Update(num_Score);
		scoreArr[NUMBER_LIFE]->Update(num_Life);
		scoreArr[COIN]->Update(num_Coin);

		xPointMenu = 320;
		yPointMenu = 320;
		ChangeState(GS_PLAYING);
		break;

	case GS_ABOUT:

		break;

	case GS_GAMEOVER:
		map_Current = 1;	//Neu choi tiep thi choi tu map 1
		break;

	case GS_NEXT_STAGE:
		reset_Time = 0;
		timeChangeMap = 30;
		break;

	case GS_RETURN_STAGE:
		
		reset_Time = 0;
		timeChangeMap = 10;
		break;

	case GS_REPLAY:
		reset_Time = 0;
		timeChangeMap = 30;
		break;

	case GS_SUB_STAGE:
		reset_Time = 0;
		timeChangeMap = 10;
		/*submap = map_Current;
		ChangeSubMap(submap);*/
		break;
	case GS_PAUSE:
		xPointMenu = 320;
		yPointMenu = 320;
		break;
	case GS_SAVE:
		
		break;
	}
}

void MarioGame::ChangeMap(int _map)
{
	/*if (issubmap == true)
	{
	mapManager->isLoadSubMap = true;
	issubmap = false;
	}*/
	int mario_state = 0;
	map_Current = _map;
	switch(map_Current)
	{
	case 1:
		delete quadtree;
		quadtree = new QuadTree();
		//delete load_QTree;
		//load_QTree = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler);
		mario_state = load_QTree->mario_state;
		load_QTree->obj_list.clear();
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\map.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\map.txtquadtree.txt");
		load_QTree->deserialize(quadtree);
		 
		if (load_QTree->submapToMap == true)
		{
			load_QTree->submapToMap = false;
			//_mario = new Mario(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_QTree->posX_last, load_QTree->posY_last, this, mario_state);
			_mario->setObjState(mario_state);
			
			_mario->setPosX(load_QTree->posX_last);
			_mario->setPosY(load_QTree->posY_last);
		}
		else
		{
			_mario = new Mario(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_QTree->posX_first, load_QTree->posY_first, this, load_QTree->mario_state, load_QTree->mario_direction);
			
		}
			
		
		 
		
		
		
		break;

	case 2:
		delete quadtree;
		quadtree = new QuadTree();
		 
		load_QTree->obj_list.clear();
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\map.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\map.txtquadtree.txt");
		load_QTree->deserialize(quadtree);
		 

		obj_list.clear();

		if (load_QTree->submapToMap == true)
		{
			load_QTree->submapToMap = false;
			_mario->setPosX(load_QTree->posX_last);
			_mario->setPosY(load_QTree->posY_last);			
		}
		else
		{
			_mario->setPosX(load_QTree->posX_first);
			_mario->setPosY(load_QTree->posY_first);			
		}
		
		break;

	case 3:
		delete quadtree;
		quadtree = new QuadTree();
		 
		load_QTree->obj_list.clear();
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\map.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\map.txtquadtree.txt");
		load_QTree->deserialize(quadtree);

		 
		if (load_QTree->submapToMap == true)
		{
			load_QTree->submapToMap = false;
			_mario->setPosX(load_QTree->posX_last);
			_mario->setPosY(load_QTree->posY_last);			
		}
		else
		{
			_mario->setPosX(load_QTree->posX_first);
			_mario->setPosY(load_QTree->posY_first);			
		}
		
		break;
	}
	 
	
	 

	if(map_Current < 4)
		ChangeState(GS_PLAYING);
	else
	{
		ChangeState(GS_GAMEWIN);
		_mario->setPosX(100);
		_mario->setPosY(180);
	}

}


void MarioGame::ChangeSubMap(int _submap)
{

	subMapToMap = 1;

	submap = _submap;
	switch(submap)
	{
	case 1:	
		load_QTree->mario_state = _mario->getObjState();
		load_QTree->mario_direction = _mario->getDirection();

		delete quadtree;
		quadtree = new QuadTree();		
		load_QTree->obj_list.clear();		
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_QTree->deserialize(quadtree);
		load_QTree->submapToMap = true;		

		
		_mario->setPosX(load_QTree->posX_first);
		_mario->setPosY(load_QTree->posY_first);
		break;

	case 2:		
		delete quadtree;
		quadtree = new QuadTree();
		load_QTree->obj_list.clear();

		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_QTree->deserialize(quadtree);

		_mario->setPosX(load_QTree->posX_first);
		_mario->setPosY(load_QTree->posY_first);
		break;

	case 3:
		delete quadtree;
		quadtree = new QuadTree();
		load_QTree->obj_list.clear();
		obj_list.clear();
		load_QTree->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_QTree->loadFileToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_QTree->deserialize(quadtree);

		_mario->setPosX(load_QTree->posX_first);
		_mario->setPosY(load_QTree->posY_first);
		break;
	}


	if(submap < 4)
		ChangeState(GS_PLAYING);
	else 
		ChangeState(GS_GAMEWIN);

}